import axios from 'axios';
import store from '../../store';

const isDev = process.env.NODE_ENV === 'development';

// 仅适用于本地开发时 存在一个本地代理的地址 如果代理多个地址 需要改动
function getBaseUrl() {
  let baseUrl;
  if (isDev) {
    const projectAddress = ` http://172.16.18.35:${process.env.VUE_APP_PORT}`;
    const proxyPrefix = process.env.VUE_APP_API_URL_PROXY_PREFIX;
    if (proxyPrefix) {
      baseUrl = projectAddress + proxyPrefix;
    } else {
      baseUrl = projectAddress;
    }
  } else {
    baseUrl = process.env.VUE_APP_PROJECT_URL;
  }

  return baseUrl;
}
// debugger;
const service = axios.create({
  timeout: 1000 * 30,
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  },
  baseURL: process.env.NODE_ENV === 'development' ? '/api' : process.env.VUE_APP_REQUEST_BASE_URL
});

service.interceptors.request.use(
  config => {
    config.headers.Authorization = store.state.user.Token
      ? 'bearer ' + store.state.user.Token
      : 'bearer 25d70959-7f39-41f6-94ca-93c5337ca202';
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  res => {
    return res;
  },
  error => {
    return Promise.reject(error.response);
  }
);

export default service;
